<%@ page language="java" import="util.*, java.sql.*, java.io.*, java.util.*, entity.*" %>
<html><head>
        <Title>Search Page</Title>
    </head>
    <body>
        <center><h3>Search Result</h3></center>
        <table align="center" border="1">
            <tr bgcolor="white"><b>
                    <td>Item ID:</td>
                    <td>Name</td>
                    <td>Type</td>
                    <td>Company</td>
                    <td>Scale</td>
                    <td>Price</td>
                    <td>Description</td>
                    <td>Image</td>
                    <td>Quantity</td>
                <td>Buy</td></b>
            </tr>
            
            <%
            /*String categoryID = request.getParameter("cat");
            if(categoryID != null) {
                List<Book> books = BookService.getByCategory(categoryID);
                }*/

            String keyword = request.getParameter("keyword");
            Connection con = null;
            int totalRow = 0;
            try {
                int rowsPerPage = 5;
                int rowCount = 0;
                boolean more = false;

                int nextRow = 0;
                String next = request.getParameter("previous");
                if (Utilities.isValid(next)) {
                    nextRow = Integer.parseInt(next);
                }

                con = ConnectionManager.connect();
                String query = ("SELECT * FROM model WHERE name LIKE '%" + keyword + "%' OR type LIKE '%" + keyword + "%'" +
                        "OR description LIKE '%" + keyword + "%' OR scale LIKE '%" + keyword + "%' OR company LIKE '%" + keyword + "%'ORDER BY itemID;");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);

                for (int i = 0; i < nextRow; i++) {
                    rs.next();
                }

                while (rs.next()) {
                    rowCount++;

                    String itemId = rs.getString("itemID");
                    String name = rs.getString("name");
                    String type = rs.getString("type");
                    String company = rs.getString("company");
                    String scale = rs.getString("scale");
                    String price = rs.getString("price");
                    String description = rs.getString("description");
                    String imageURL = rs.getString("imageURL");
            %>
            <form action='savecart.jsp' method='get'>
                <tr>
                    <td>
                        <%=itemId%>
                        <input type='hidden' name='itemID' value='<%=itemId%>' />
                    </td>
                    <td>
                        <%=name%>
                        <input type='hidden' name='name' value='<%=name%>' />
                    </td>
                    <td>
                        <%=type%>
                        <input type='hidden' name='type' value='<%=type%>' />
                    </td>
                    <td>
                        <%=company%>
                        <input type='hidden' name='company' value='<%=company%>' />
                    </td>
                    <td>
                        <%=scale%>
                        <input type='hidden' name='scale' value='<%=scale%>' />
                    </td>
                    <td>
                        $<%=price%>
                        <input type='hidden' name='price' value='<%=price%>' />
                    </td>
                    <td>
                        <%=description%>
                        <input type='hidden' name='description' value='<%=description%>' />
                    </td>
                    <td>
                        <a href='photo.jsp?imageURL=<%=imageURL%>'><img src='../image/<%=imageURL%>' width=150 height=200/></a>
                        <input type='hidden' name='imageURL' value='<%=imageURL%>' />
                    </td>
                    <td>
                        <input type='TEXT' size='5' name='quantity' value='1'>                        
                    </td>
                    <td valign='top'>
                        <input type='Submit' value='Buy'>
                    </td>        
                </tr>
            </form>
            <%
                    if ((rowsPerPage > 0) && (rowCount >= rowsPerPage)) {
                        // Find out if there are any more rows after this one
                        more = rs.next();
                        totalRow = rowCount + nextRow;
                        if (more) {
                            ++totalRow;
                            while (rs.next()) {
                                ++totalRow;
                            }
                        }
                        break;
                    }
                }
            %>
        </table>
        <%
                if (more) {
        %>
        <form method='get' action='search.jsp'>
            <center>
                <input type='hidden' name='keyword' value='<%=keyword%>' /> 
                <input type='hidden' name='previous' value='<%=rowCount + nextRow%>' />
                <%
            int nextValue = 5;
            int remain = totalRow - (rowCount + nextRow);
            if (remain < rowsPerPage) {
                nextValue = remain % rowsPerPage;
            }

            int page2 = 0;
            String page1 = request.getParameter("page");
            try {
                if (Utilities.isValid(page1)) {
                    page2 = Integer.parseInt(page1);
                }
            } catch (Exception e) {
                out.println(e);
            }
            int begin = 1, end = 10;
            int totalPage = (int) Math.ceil(totalRow / rowsPerPage);

            if (page2 < 7) {
            } else if (page2 > totalPage - 5) {
                begin = totalPage - 9;
                end = totalPage - 1;
            } else {
                begin = page2 - 5;
                if (page2 + 4 > totalPage) {
                    end = totalPage - 1;
                } else {
                    end = page2 + 4;
                }
            }

            int j = begin;
            for (int i = begin; i <= end; i++) {
                %>
                <a href='search.jsp?keyword=<%=keyword%>&amp;page=<%=j%>&amp;previous=<%=i * rowsPerPage%>'><%=j++%></a>
                <%
            }
                %>
                <input type='hidden' name='page' value='<%=j%>' />
                <input type = submit value='Next <%=nextValue%> rows'/>
            </center>
        </form>
        <%
                }
            } catch (Exception e) {
            } finally {
                con.close();
            }
        %>
        
    </body>
</html>		